let mongoose = require('mongoose'),
    async = require('async'),
    resoucesPost = mongoose.model('resouces_post');

module.exports = (req, res) => {
    let params = req.body,
        status = false, msg = '';

    switch(true){
        case !params._id:
            msg = '请传入资源ID';
            status = true;
            break;
        case !params.title:
            msg = '请传入资源标题';
            status = true;
            break;
        case !params.content:
            msg = '请传入资源内容';
            status = true;
            break;
        default: break;
    }
    if(status){
        res.json({
            result: 0,
            message: msg
        })
    }

    async.series([
        // 查询
        callback => {
            resoucesPost.find({
                _id: params._id
            }, (err, data) => {
                if(err){
                    res.json(resFail('查询出错'))
                }else{
                    if(!data.length){
                        res.json(resFail('资源不存在'))
                    }else{
                        callback();
                    }
                }
            })
        },
        // 修改
        callback => {
            resoucesPost.update({
                _id: params._id
            }, {
                $set: {
                    title: params.title,
                    content: params.content
                }
            }, false).exec((err, data) => {
                if(err){
                    res.json(resFail('更新出错'))
                }else{
                    res.json({
                        result: 1,
                        message: '更新成功！'
                    })
                }
            })
        }
    ])
}